package app

import (
	"bookkeeping-gin/app/common/request"
	"bookkeeping-gin/app/common/response"
	"bookkeeping-gin/app/service"
	"github.com/gin-gonic/gin"
	"github.com/golang-jwt/jwt/v5"
	"log"
)

// Register godoc
//
//	@Summary		Register
//	@Description	Register
//	@Tags			User
//	@Accept			json
//	@Produce		json
//	@Param			form	body		request.RegisterFormWithPwd	true	"RegisterForm"
//	@Success		200		{object}	service.TokenOutPut
//	@Router			/user/register [post]
func Register(c *gin.Context) {
	var form request.RegisterFormWithPwd
	if err := c.ShouldBindJSON(&form); err != nil {
		log.Println("Register err:", err)
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	if err, user := service.UserService.Register(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, user)
	}
}

// Login godoc
//
//	@Summary		Login
//	@Description	Login
//	@Tags			User
//	@Accept			json
//	@Produce		json
//	@Param			form	body		request.LoginForm	true	"LoginForm"
//	@Success		200		{object}	model.User
//	@Router			/user/login [post]
func Login(c *gin.Context) {
	var form request.LoginForm
	if err := c.ShouldBindJSON(&form); err != nil {
		log.Println("Login err:", err)
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	if err, user := service.UserService.Login(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		tokenData, err, _ := service.JwtService.CreateToken(service.AppGuardName, user)
		if err != nil {
			response.BusinessFail(c, err.Error())
			return
		}
		response.Success(c, tokenData)
	}
}

// Logout godoc
//
//	@Security		JWT
//	@Summary		Logout
//	@Description	Logout
//	@Tags			User
//	@Accept			json
//	@Produce		json
//	@Security		ApiKeyAuth
//	@Success		200
//	@Router			/user/logout [post]
func Logout(c *gin.Context) {
	err := service.JwtService.JoinBlackList(c.Keys["token"].(*jwt.Token))
	if err != nil {
		response.BusinessFail(c, "登出失败")
		return
	}
	response.Success(c, "登出成功")
}

// GetMeInfo godoc
//
//	@Security		JWT
//	@Summary		Get current user info
//	@Description	Get user info by ID
//	@Tags			User
//	@Accept			json
//	@Produce		json
//	@Success		200	{object}	model.User
//	@Router			/user/me [get]
func GetMeInfo(c *gin.Context) {
	if err, user := service.UserService.GetUserInfoById(c.Keys["userid"].(string)); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, user)
	}
}

// GetUserInfoById godoc
//
//	@Security		JWT
//	@Summary		Get user info by ID
//	@Description	Get user info by ID, if id is empty, use current user id as default.
//	@Tags			User
//	@Accept			json
//	@Produce		json
//	@Param			id	query		int	true	"User ID"
//	@Success		200	{object}	model.User
//	@Router			/user/info [get]
func GetUserInfoById(c *gin.Context) {
	id := c.Query("id")
	// 如果id为空，使用当前用户id
	if id == "" {
		id = c.Keys["userid"].(string)
	}
	if err, user := service.UserService.GetUserInfoById(id); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, user)
	}
}
